# 模块导入
from . import Login
from . import database


# Login表的mapper
# 测试完成
class LoginMapper:
    # query
    # 根据id查找login项
    # <Login 1>
    @staticmethod
    def query_id(id):
        return Login.query.get(id)

    # 查找login列表
    # [<Login 1>, <Login 2>, ...]
    @staticmethod
    def query_all():
        return Login.query.all()

    # 根据邮箱查询login项
    # <Login 1>
    @staticmethod
    def query_email(email):
        print(Login.query.filter_by(email=email).first())
        return Login.query.filter_by(email=email).first()

    # 根据条件联合查询login项
    # <Login 1>
    @staticmethod
    def query_email_password(email, password):
        return Login.query.filter_by(email=email, password=password).first()

    # insert
    # 增加一个login项
    @staticmethod
    def insert(email, password, is_admin):
        login = Login(email=email, password=password, is_admin=is_admin)
        database.session.add(login)
        database.session.commit()
        return "insert success"

    # update
    # 更新login项
    @staticmethod
    def update(user_id, email, password, is_admin):
        login = Login.query.get(user_id)
        login.email = email
        login.password = password
        login.is_admin = is_admin
        database.session.commit()
        return "update success"

    # 更新login项的password
    @staticmethod
    def update_password(user_id, password):
        login = Login.query.get(user_id)
        login.password = password
        database.session.commit()
        return "update success"

    # 更新login项的password
    @staticmethod
    def update_is_admin(user_id, is_admin):
        login = Login.query.get(user_id)
        login.is_admin = is_admin
        database.session.commit()
        return "update success"

    # delete
    # 删除login项
    @staticmethod
    def delete(id):
        login = Login.query.get(id)
        database.session.delete(login)
        database.session.commit()
        return "delete success"

